﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.Framework.RPC.Sockets;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 玩家从座位站起
	/// (根据客户端提交的桌子号，座位号，UserID来判断当期位置是否有人，如果有人则返回当前玩家ID，如果信息正确则将玩家从座位移出并且清空座位筹码)
	/// </summary>
	public class Action20009: BaseAction
	{
		private int _userId;
		private int _tableId;
		private int _posId;  //1-9
		private bool _result = false;
		private TableData _currentTable;
		private GameUser _currentGameUser;
		private PositionData _currentPos;

		public Action20009(HttpGet actionGetter)
			: base((short)ActionType.DZGameUserStandUp, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref _tableId) &&
				httpGet.GetInt("UserID", ref _userId) &&
				httpGet.GetInt("PosID", ref _posId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			try
			{
				_currentTable = GameRoom.Current.GetTableData(_tableId);
				if (_currentTable == null)
				{
					Tool.WriteWarmingLine("消息20009获得房间不存在");
					return false;
				}
				if (_posId > 9 || _posId < 1)
				{
					Tool.WriteWarmingLine("消息20009不合法");
					return false;
				}
				var pos = _currentTable.Positions[_posId - 1];
				if (pos == null)
				{
					Tool.WriteWarmingLine("消息20009座位号不存在");
					return false;
				}
				_currentGameUser = GameTable.Current.GetUser(_userId);
				if (_currentGameUser == null)
				{
					Tool.WriteWarmingLine("消息20009不存在当前玩家");
					return false;
				}
				if (pos.IsEmpty)
				{
					Tool.WriteWarmingLine("消息20009前位置没有人");
					return false;
				}
				else
				{
					if (pos.UserId != _currentGameUser.GetUserId())
					{
						Tool.WriteWarmingLine("当前位置不是这个玩家");
						return false;
					}
				}
				if (pos.PlayState == PositionPlayState.Add || pos.PlayState == PositionPlayState.AllIn || pos.PlayState == PositionPlayState.Follow || pos.PlayState == PositionPlayState.Look)
				{
					Tool.WriteLine("20009玩家站起失败，因为玩家正在游戏中");
					return false;
				}
				this._currentPos = pos;
				this._result = true;
				return true;
			}
			catch (Exception ex)
			{
				Tool.WriteErrorLine("20009消息异常:" + ex.ToString());
				return false;
			}
		}

		public override void TakeActionAffter(bool state)
		{
			if (_result)
			{
				if (!_currentTable.StandUp(this._currentPos, _currentGameUser))
				{
					Tool.WriteErrorLine("消息20009 玩家站起有异常");
					this._result = false;
				}
				this._result = true;
			}
			base.TakeActionAffter(state);
		}

		public override void BuildPacket()
		{
			//返回是否成功
			this.PushIntoStack(this._result);
			if(_result)
			{
				var parameters = new Parameters();
				parameters["TableID"] = _tableId;
				parameters["UserID"] = _userId;
				parameters["PosID"] = _posId;
				GameTable.Current.SyncNotifyAction((int)ActionType.DZBrocastGameUserLeaveSet, _currentTable, parameters, t =>
				{
					Console.WriteLine("Action 20010玩家站起");
				});
			}
		}
	}
}
